<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 加密数据
if (!function_exists('_encrypted')) {
    function _encrypted($params)
    {
//        $path = Env::get('extend_path');
//        $pub_path = $path . "openssl\public_key.php";
//        $public_key = include_once $pub_path;
//        //先序列号再base64加密
//        $serialize = serialize($params);
//        $data = base64_encode($serialize);
//        openssl_private_encrypt($data, $encrypted, $public_key);
//        $encrypted = base64_encode($encrypted); // 避免特殊字符
//        return $encrypted;
    }
}
// 成功返回
if (!function_exists('apiReturnSuc')) {
    function apiReturnSuc($data = [], $msg = 'success', $code = 1)
    {
        //$data = _encrypted($data);
        $data = ['data' => $data, 'msg' => $msg, 'code' => $code];
        return json($data);
    }
}
// 失败返回
if (!function_exists('apiReturnFail')) {
    function apiReturnFail($msg = '', $data = [], $code = 0)
    {
        $data = ['data' => $data, 'msg' => $msg, 'code' => $code];
        return json($data);
    }
}
// api返回
if (!function_exists('apiReturns')) {
    function apiReturns()
    {
        $instance = \app\common\helper\ApiReturn::getInstance();
        return $instance->apiReturns();
    }
}

// 根据数组转换的参数值
if (!function_exists('get_arr')) {

    function get_arr($data, $num = '0')
    {
        $arr = [];
        foreach ($data as $k => $v) {
            if (is_numeric($k)) {
                $arr[] = [
                    'label' => $v,
                    'value' => $k + $num
                ];
            } else {
                $arr[] = [
                    'label' => $v,
                    'value' => $k
                ];
            }
        }

        return $arr;
    }
}

// 树形菜单
if (!function_exists('get_tree')) {

    function get_tree($array, $parent_id = 0)
    {
        static $tree = [];
        foreach ($array as $val) {
            if ($val['parent_id'] == $parent_id) {
                $val['child'] = get_tree($val, $val['id']);

                $tree[] = $val;
            }
        }
        return $tree;
    }
}